package com.suixun.service.credits.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;

import com.suixun.dao.credits.CreditReordMapper;
import com.suixun.dao.credits.CreditTaskMapper;
import com.suixun.entity.credits.CreditTask;
import com.suixun.service.credits.CreditReordService;
import com.suixun.vo.credits.CreditsCountVo;
import com.suixun.vo.credits.CreditsGetVo;
import com.suixun.vo.credits.CreditsReordVo;
import com.suixun.vo.credits.RequestPageVo;
import com.suixun.vo.credits.ResponseVo;

/**
 * 积分记录(明细)
 * 
 * @author yh
 * @date 2017年11月17日
 */
@Service
public class CreditReordServiceImpl implements CreditReordService {

	// 积分的记录
	@Autowired
	private CreditReordMapper creditReordDao;

	// 积分记录包括获取与消费
	@Autowired
	private CreditTaskMapper creditTaskDao;

	@Override
	public CreditsCountVo findSumInfo() {
		Map<String, Integer> map = creditReordDao.findSumInfo();
		if (map == null) {
			return null;
		}
		CreditsCountVo vo = new CreditsCountVo();
		BeanMap beanMap = BeanMap.create(vo);
		beanMap.putAll(map);
		return vo;
	}

	@Override
	public ResponseVo<CreditsReordVo> findPageByTel(RequestPageVo param, String tel) {

		Integer total = creditReordDao.findTotalByTel(tel);
		if (total < 1) {
			return new ResponseVo<>();
		}
		List<Map<String, Object>> list = creditReordDao.findPageByTel(param.getStart(), param.getLength(), tel);
		List<CreditsReordVo> listvo = new ArrayList<>();

		for (int i = 0; i < list.size(); i++) {
			CreditsReordVo vo = new CreditsReordVo();
			Map<String, Object> map = list.get(i);
			BeanMap beanMap = BeanMap.create(vo);
			beanMap.putAll(map);
			listvo.add(vo);
		}

		return new ResponseVo<>(listvo, total);
	}

	@Override
	public ResponseVo<Map<String, Object>> findPageGetInfo(CreditsGetVo param) {

		List<CreditTask> taskList = creditTaskDao.findAllName();
		if (taskList == null) {
			return null;
		}

		Integer size = taskList.size();
		Integer[] taskids = new Integer[size];
		for (int i = 0; i < size; i++) {
			taskids[i] = taskList.get(i).getId();
		}

		// 总条数
		Integer total = creditReordDao.findTotalGetInfo(param.getStartDate(), param.getEndDate());
		if (total < 1) {
			return new ResponseVo<>();
		}

		// 总记录
		List<Map<String, Object>> list = creditReordDao.findPageGetInfo(taskids, param.getStartDate(),
				param.getEndDate(), param.getStart(), param.getLength());

		return new ResponseVo<>(list, total);
	}

	@Override
	public List<Map<String, Object>> findAllGetInfo(CreditsGetVo param) {
		Integer total = creditReordDao.findTotalGetInfo(param.getStartDate(), param.getEndDate());
		if (total < 1) {
			return null;
		}
		List<CreditTask> taskList = creditTaskDao.findAllName();
		Integer size = taskList.size();
		Integer[] taskids = new Integer[size];
		for (int i = 0; i < size; i++) {
			taskids[i] = taskList.get(i).getId();
		}
		return creditReordDao.findPageGetInfo(taskids, param.getStartDate(), param.getEndDate(), 0, total);
	}

}
